<template>
  <div>
    <div class="demo-image">
    
      <el-form class="form" label-position="left" :model="form" :rules="rules" ref="form">
        <p class="title">花友日记</p>
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="onSubmit()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
    
  </div>
</template>

<script>

import httpApi from '@/http';
export default {
  data() {
    return {
      form: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [
          { required: true, message: "请填写手机号", trigger: "blur" },
          {
               pattern:/^1[3-9]\d{9}$/,
            message: "请输入合法的手机号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请填写密码", trigger: "blur" },
          {
            pattern: /^\w{6,15}$/,
            message: "6~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    onSubmit() {
      // 调用相关API执行登录业务
      httpApi.adminApi.login(this.form).then(res=>{
        console.log('登录', res)
        if(res.data.code==200){
          // 登录成功后，需要将用户对象存入vuex
          // mutations中定义了一个方法：updateUserInfo()用于更新用户信息
          // 在此需要让vuex执行该方法，更新state中的user对象
          this.$store.commit('updateUserInfo', res.data.data.user)
          this.$store.commit('updateToken', res.data.data.token)

          this.$router.push('/home/index')
        }else {
          this.$message({
            message: '账号密码错误，请重新输入',
            type: 'error'
          })
          this.$refs['form'].resetFields() // 重置表单
        }
      })
    },
  },
};
</script>

<style lang="scss" scoped>
.demo-image{
  height: 100vh;
  background-image: url('@/assets/bg.jpg');
  animation: animate 20s infinite; 
  position: relative; 
}
.form{
  display: block;
  padding: 10px 20px;
  background-color: rgba($color: #266349, $alpha: 0.8);
  width: 20%;
  position: absolute;
  top: 30%;
  left: 40%;
}
.form .title{
  text-align: center;
  font-size:2em;
  font-weight: 800;
  color: white;
  margin-bottom: 20px;
  
}


@keyframes animate {
    0%{
      background-position: 33% 53%;
    }
    50%{
      background-position: 34% 50%;
    }
    100%{
      background-position: 33% 53%;
    }
  }
</style>